<%@ page language="java" contentType="text/json; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%@page trimDirectiveWhitespaces="true"%>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page language="java" import="java.lang.*" %>
<%@ page import = "java.sql.SQLException" %>
<%@ page import = "com.seniorproject.aims.*" %>
<%@ page import = "java.util.List" %>
<%@page import="java.util.ArrayList"%>
<%@ page import = "java.util.Properties" %>
<%@ page import = "javax.servlet.ServletContext" %>
<%@ page import = "java.io.File" %>
<%@ page import = "java.io.InputStream" %>
<%@ page import = "org.codehaus.jackson.JsonGenerationException" %>
<%@ page import = "org.codehaus.jackson.map.JsonMappingException" %>
<%@ page import = "org.codehaus.jackson.map.ObjectMapper" %>
<%@ page import = "java.io.IOException" %>

<%
	//get session
	String strUser = String.valueOf(session.getAttribute("sUser"));
	HttpSession htp_session = request.getSession();
	
	// check session
	if (htp_session == null || htp_session.getAttribute("sUser") == null) {
	    // Forward the control to login.jsp if authentication fails or session expires
	    request.getRequestDispatcher("/login.jsp").forward(request,
	        response);
	}
	
	if("admin".equals(htp_session.getAttribute("sUser"))){%>
	<script>alert("This user dont have PERMISSION to access this zone.");</script>
	<meta HTTP-EQUIV="Refresh" CONTENT="0; URL=admin_home.jsp"> 	
	<%}
	
	System.out.println("@ search courseid 5555555555555555555555 ");////////////try//////////

	//get parameter
	String courseid = request.getParameter("courseid");
	String courseid_full = courseid.toUpperCase();
			
	System.out.println("courseid : "+courseid);////////////try//////////
	System.out.println("courseid_full : "+courseid_full);////////////try//////////
	//set Database Connection
	String hostProps = "";
	String usernameProps  = "";
	String passwordProps  = "";
	String databaseProps = "";
	
	try {
		//get current path
		ServletContext servletContext = request.getSession().getServletContext();
		
		InputStream input = servletContext.getResourceAsStream("/properties/connectDB.properties");
		Properties props = new Properties();
		
		props.load(input);
	
		hostProps  = props.getProperty("host");
		usernameProps  = props.getProperty("username");
		passwordProps  = props.getProperty("password");
		databaseProps = props.getProperty("database");
	} catch (Exception e) { 
		out.println(e);  
	}
	
	// connect database
	Connection connect = null;		
	try {
		Class.forName("com.mysql.jdbc.Driver");
	
		connect =  DriverManager.getConnection("jdbc:mysql://" + hostProps  + "/" + databaseProps +
				"?user=" + usernameProps  + "&password=" + passwordProps + "&characterEncoding=tis620" );
	
		if(connect != null){
			System.out.println("Database Connect Sucesses.");
		} else {
			System.out.println("Database Connect Failed.");	
		}
	
	} catch (Exception e) {
		out.println(e.getMessage());
		e.printStackTrace();
	}
	
	// search name in database
	List<Course_Name> coursename = new ArrayList<Course_Name>();
	String Json="";
	ObjectMapper mapper = new ObjectMapper();
	
	try {			   
		ResultSet rs =  connect.createStatement().executeQuery("SELECT `course_id` , `course_name_en`, `course_name_th` , `credit`, `lecture_hour`, `lab_hour`, `self_learning_hour`, `detail`"
					+ " FROM `course_name` "
					+ " WHERE course_id LIKE '"+ courseid + "%' or course_id LIKE '"+ courseid_full + "%'" );
		
		while(rs.next()) {
			
			Course_Name course = new Course_Name();
			
			course.setCourseId(rs.getString("course_id"));
			course.setCourseNameEN(rs.getString("course_name_en"));
			course.setCredit(rs.getInt("credit"));
			//course.setCourseNameTH(rs.getString("course_name_th"));
			course.setDetails(rs.getString("detail"));
			course.setLab(rs.getInt("lab_hour"));
			course.setLecture(rs.getInt("lecture_hour"));
			course.setSelfLearn(rs.getInt("self_learning_hour"));
			
			coursename.add(course);
			if(coursename.size()==10)break;
			//System.out.println("COURSE SIZE:  "+coursename.size());
		}
		

		try {
			Json = mapper.writeValueAsString(coursename); 
		} catch (JsonGenerationException ex) {
			ex.printStackTrace();
		} catch (JsonMappingException ex) {
			ex.printStackTrace();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		
		out.print(Json);
			
	} catch (SQLException e) {
		e.printStackTrace();
	}

	connect.close();
%>